<?php
session_start();
if (!isset($_SESSION['admin']))
    header('location:index.php');
?>

<!DOCTYPE HTML>
<html>

    <head>
        <title>3D|didongde 3D- Di động đây</title>
        <?php include('modules/page_head.php'); ?>
    </head>

    <body>
        <div id="main">
            <div id="header">
                <div id="logo">
                    <h1><a href="index.php">Didongde</a></h1>
                    <div class="slogan">Where you can find your own best suitable mobile</div>
                    <?php include('modules/logo_header.php'); ?>
                </div>
                <div id="menubar">
                    <?php include('modules/menubar.php'); ?>
                </div>
            </div>
        </div>
        <div id="site_content">
            <div id="sidebar_container">
                <h2>Tìm kiếm nhanh</h2>
                <div class="sidebar">
                    <?php
                    include ("modules/quick_search.php");
                    show_manu();
                    ?>
                </div>
                <div class="sidebar">
                    <?php show_price(); ?>
                </div>
            </div>
            <div id="content">
                <!-- content of this site here -->
                <h2>Thêm nhà cung cấp</h2>
                <div>
                    <?php
                    if (isset($_POST['submit'])) {
                        /* connection to database server */
                        include_once('conf/dbconfig.php');
                        $cfg = new dbconfig();
                        $host = $cfg->get_dbhost();
                        $db = $cfg->get_dbname();
                        $usr = $cfg->get_dbusername();
                        $pass = $cfg->get_dbpwd();
                        $connection = pg_connect("host = $host dbname = $db user = $usr password = $pass")
                                or die("Cannot connect to database server: " . pg_last_error());

                        /* process to add a new manufacturer */
                        $name = $_POST['manufacturer'];
                        $email = $_POST['email'];
                        $phone = $_POST['phonenumber'];
                        $addr = $_POST['address'];
                        $query = "select * from manufacturer";
                        $result = pg_query($connection, $query);
                        $manuid = pg_num_rows($result);
                        $manuid = strval($manuid + 1);
                        while (strlen($manuid) < 3)
                            $manuid = "0" . $manuid;
                        $manuid = "M" . $manuid;
                        $query = "insert into manufacturer (manuid, name, email, address, phone_number) values ('$manuid', '$name', '$email', '$addr', '$phone')";
                        $result = pg_query($connection, $query);
                        if (pg_affected_rows($result) > 0)
                            echo "<h6>Thêm thành công nhà cung cấp $name</h6>";
                        else
                            echo "<h4>*Có lỗi xảy ra khi thêm nhà cung cấp. Hãy kiểm tra và thử lại!</h4>";
                    }
                    ?>
                </div>
                <div class="form_settings">
                    <form name="add_manu" method="post" action="#">
                        <table>
                            <tr><td>Tên nhà cung cấp</td><td>
                                    <input type="text" name="manufacturer" placeholder="Nhập tên nhà cung cấp"/>
                                </td></tr>
                            <tr><td>Địa chỉ email</td><td>
                                    <input type="email" name="email" placeholder="Nhập địa chỉ email" />
                                </td></tr>
                            <tr><td>Địa chỉ</td><td>
                                    <input type="text" name="address" placeholder="Nhập địa chỉ" />
                                </td></tr>
                            <tr><td>Số điện thoại</td><td>
                                    <input type="tel" name="phonenumber" placeholder="Nhập số điện thoại" />
                                </td></tr>
                            <tr><td colspan="2"><input class="submit" name="submit" type="submit" value="Thêm nhà cung cấp" style="padding:3px 4px 3px 4px;float: right;"/></td>
                            </tr>
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div id="footer">
        <?php include('modules/footer.php'); ?>
    </div>
</body>
</html>

